package main

import (
	xssh "golang.org/x/crypto/ssh"
	"io"
	"log"
	"os"
	"time"
)

func main() {

	/*
		sshClient, err := ssh.DialWithDefault("192.168.237.41", "netadmin", "Trust@12345")
		if err != nil {
			log.Fatal(err)
		}

		cmd := ssh.Command{
			Cmd: "screen-length disable",
		}
		sshClient.SendCommand(cmd)

	*/
}

func test() {

	conf := xssh.ClientConfig{
		User:            "netadmin",
		Auth:            []xssh.AuthMethod{xssh.Password("Trust@12345")},
		HostKeyCallback: xssh.InsecureIgnoreHostKey(),
	}

	// 建立SSH客户端连接
	// 192.168.237.44
	// ssh-old-giano-bak netadmin@192.168.237.41
	client, err := xssh.Dial("tcp", "192.168.237.41:22", &conf)
	if err != nil {
		log.Fatalf("SSH dial error: %s", err.Error())
	}

	// 建立新会话
	session, err := client.NewSession()
	defer session.Close()
	if err != nil {
		log.Fatalf("new session error: %s", err.Error())
	}

	var echoReader io.Reader
	var cmdWriter io.WriteCloser
	echoReader, err = session.StdoutPipe()
	cmdWriter, err = session.StdinPipe()

	modes := xssh.TerminalModes{
		xssh.ECHO:          0,     // 禁用回显（0禁用，1启动）
		xssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
		xssh.TTY_OP_OSPEED: 14400, //output speed = 14.4kbaud
	}
	if err = session.RequestPty("vt102", 128, 160, modes); err != nil {
		log.Fatalf("request pty error: %s", err.Error())
	}
	if err = session.Shell(); err != nil {
		log.Fatalf("start shell error: %s", err.Error())
	}

	time.Sleep(10 * time.Second)

	buf := make([]byte, 1024*64*64)
	echoReader.Read(buf)
	log.Printf("begin==========")
	log.Printf("%s", buf)

	log.Printf("send screen-length disable==========")
	cmdWriter.Write([]byte("screen-length disable\n"))
	buf = make([]byte, 1024*64*64)

	time.Sleep(30 * time.Second)
	echoReader.Read(buf)
	log.Printf("%s", buf)

	log.Printf("send dis int brief==========")
	cmdWriter.Write([]byte("dis int brief\n"))
	buf = make([]byte, 1024*64*64)

	time.Sleep(60 * time.Second)
	echoReader.Read(buf)
	log.Printf("%s", buf)

	if err = session.Wait(); err != nil {
		log.Fatalf("return error: %s", err.Error())
	}

}

func shell() {
	conf := xssh.ClientConfig{
		User:            "netadmin",
		Auth:            []xssh.AuthMethod{xssh.Password("Trust@12345")},
		HostKeyCallback: xssh.InsecureIgnoreHostKey(),
	}

	// 建立SSH客户端连接
	client, err := xssh.Dial("tcp", "192.168.237.44:22", &conf)
	if err != nil {
		log.Fatalf("SSH dial error: %s", err.Error())
	}

	// 建立新会话
	session, err := client.NewSession()
	defer session.Close()
	if err != nil {
		log.Fatalf("new session error: %s", err.Error())
	}

	session.Stdout = os.Stdout // 会话输出关联到系统标准输出设备
	session.Stderr = os.Stderr // 会话错误输出关联到系统标准错误输出设备
	session.Stdin = os.Stdin   // 会话输入关联到系统标准输入设备
	modes := xssh.TerminalModes{
		xssh.ECHO:          0,     // 禁用回显（0禁用，1启动）
		xssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
		xssh.TTY_OP_OSPEED: 14400, //output speed = 14.4kbaud
	}
	if err = session.RequestPty("vt102", 32, 160, modes); err != nil {
		log.Fatalf("request pty error: %s", err.Error())
	}
	if err = session.Shell(); err != nil {
		log.Fatalf("start shell error: %s", err.Error())
	}
	if err = session.Wait(); err != nil {
		log.Fatalf("return error: %s", err.Error())
	}
}
